<style type="text/css">
#sidebar{display:none;}
#content{width:960px;}

#cartListDiv table{width:100%; text-align:left;}
#cartListDiv table th, #cartListDiv table td{padding:5px; text-align:left; vertical-align:top;
	background:#f3f3f3;}
#cartListDiv table td .goodThumbDiv, #cartListDiv table td .goodThumbDiv a{float:left; width:80px; height: 80px;}
#cartListDiv table td .goodTitleAttrDiv{float:left; width:480px; height: 80px;}
#cartListDiv table td p.goodTitle{padding:5px;}	
#cartListDiv table td p.goodAttr{padding:5px; color:#999;}	
#cartListDiv table td.deleteCartTd{width:28px;}
#cartListDiv table #cartTotalTd{text-align:right;}
#cartListDiv table #cartTotalTd span{color:#F30;}
/*#cartListDiv table #cartTotal{color:#F30;}*/
#cartListDiv table #checkOrderTd{text-align:right; background:#fff;}
#cartListDiv table #checkOrderTd a{display:inline-block; width:80px; padding:10px; text-align:center; color:#fff; background:#06f;}
</style>

<div id="cartListDiv">
	<table>
    	<thead>
        	<tr>
            	<th>商品</th>
        		<th>价格</th>
            	<th>数量</th>
        		<th>小计</th>
                <th></th>
            </tr>
        </thead>
        
        <tbody>
        	<?php foreach ($view_data['cart_list_info']['cart_list'] as $item): ?>
        	<tr id="cartItem-<?php echo $item['id']; ?>">
            	<td>              	
                    <div class="goodThumbDiv">
                        <a title="<?php echo $item['good_name']; ?>" target="_blank" href="<?php echo $this->get_url(array('route' => 'good_info', 'id' => $item['good_id']), true); ?>">
                            <img class="goodThumb" height="80" width="80" alt="<?php echo $item['good_name']; ?>" src="<?php echo $item['good_thumb']; ?>">
                            <?php //echo $item['good_thumb']; ?>
                        </a>
                    </div>
                    <div class="goodTitleAttrDiv">
                        <p class="goodTitle"><a href="<?php echo $this->get_url(array('route' => 'good_info', 'id' => $item['good_id']), true); ?>" target="_blank" ><?php echo $item['good_name']; ?></a></p>
                        <p class="goodAttr"><?php echo $item['spec_desc']; ?></p>
                    </div>
                </td>
                <td class="cartItemGoodPrice"><?php echo $item['good_price']; ?></td>
                <td>
                	<input id="goodNumber-<?php echo $item['id']; ?>" name="goodNumber-<?php echo $item['id']; ?>" type="text" value="<?php echo $item['good_number']; ?>" onblur="changeNumber('<?php echo $item['id']; ?>')" />
                	<input id="originNumber-<?php echo $item['id']; ?>" name="originNumber-<?php echo $item['id']; ?>" type="hidden" value="<?php echo $item['good_number']; ?>" />
                </td>
                <td><!--<span>￥ </span>--><span class="cartItemTotal"><?php echo $item['total']; ?></span></td>
                <td class="deleteCartTd"><a href="javascript:void(0);" onclick="deleteCart('<?php echo $item['id']; ?>');">删除</a></td>
            </tr>
            <?php endforeach; ?>
            
            <tr>
            	<td id="cartTotalTd" colspan="5">共 <span>￥ </span><span id="cartTotal"><?php echo $view_data['cart_list_info']['total']; ?></span></td>
            </tr>
            
            <tr>
            	<td id="checkOrderTd" colspan="5"><a id="checkOrder" href="javascript:void(0);" onclick="checkOrderInfo();">去下单</a></td>
            </tr>
        </tbody>
    </table>
</div>

<script type="text/javascript">
function checkOrderInfo(){
	$.ajax({
		type: 'POST',
		url: '<?php echo $this->get_url(array('route' => 'order_check'), true); ?>',
		data: '',
		dataType: 'json',
		success: function(rdata){
			if(rdata.status <= 0){
				//alert(rdata.message);

                if(rdata.status == -404){
                    show_prompt_message(rdata.message, function(){
                        window.top.location.replace('<?php echo $this->get_url(array('route' => 'cart_index'), true); ?>');
                    });
                }else{
                    show_prompt_message(rdata.message);
                }
			}else {
                //跳到订单下单页面
                var url = '<?php echo $this->get_url(array('route' => 'order_index'), true); ?>';
                window.location.replace(url);
            }

			return false;
		}
	});

	return false;
}


function changeNumber(id){
	var goodNumberId     = '#goodNumber-' + id;
	var goodNumberIdObj  = $(goodNumberId);	
	var goodNumber       = goodNumberIdObj.val();
	var originNumberId    = '#originNumber-' + id;
	var originNumberIdObj = $(originNumberId);
	var originNumber      = originNumberIdObj.val();
	if(goodNumber != originNumber){
		var postData    = 'id=' + id + '&good_number=' + goodNumber;
				
		$.ajax({
			type: 'POST',
			url: "<?php echo $this->get_url(array('route' => 'cart_change_number'), true); ?>",
			data: postData,
			dataType: 'json',
			success: function(rdata){	
				if(rdata.status <= 0){				
					//alert(rdata.message);

                    if(rdata.status == -404){
                        show_prompt_message(rdata.message, function(){
                            window.top.location.replace('<?php echo $this->get_url(array('route' => 'cart_index'), true); ?>');
                        });
                    }else{
                        show_prompt_message(rdata.message);
                    }
				}else{
					//alert(rdata.message);
                    show_prompt_message(rdata.message);
					
					var newItemGoodPrice = rdata.data.new_item_good_price;
                    var newItemTotal    = rdata.data.new_item_total;
					var changeItemTotal = parseFloat(rdata.data.change_item_total);
					
					originNumberIdObj.val(goodNumber);
					
					var cartItem = $('#cartItem-' + id);
                    cartItem.find('.cartItemGoodPrice').html(newItemGoodPrice);
                    cartItem.find('.cartItemTotal').html(newItemTotal);
					
					var cartTotalObj    = $('#cartTotal');
					var originCartTotal = parseFloat(cartTotalObj.html());					
					var newCartTotal    = originCartTotal + changeItemTotal;
					cartTotalObj.html(newCartTotal.toFixed(2));					
				}
			}
		});
	}
}

function deleteCart(id){
	//if(confirm('确定删除?')){
    confirm_prompt_message('确定删除?', function(){
		var postData    = 'id=' + id;
				
		$.ajax({
			type: 'POST',
			url: "<?php echo $this->get_url(array('route' => 'cart_delete'), true); ?>",
			data: postData,
			dataType: 'json',
			success: function(rdata){	
				if(rdata.status <= 0){				
					//alert(rdata.message);

                    if(rdata.status == -404){
                        show_prompt_message(rdata.message, function(){
                            window.top.location.replace('<?php echo $this->get_url(array('route' => 'cart_index'), true); ?>');
                        });
                    }else{
                        show_prompt_message(rdata.message);
                    }
				}else{
					//alert(rdata.message);
                    show_prompt_message(rdata.message);
					
					var changeItemTotal = parseFloat(rdata.data.change_item_total);
					
					$('#cartItem-' + id).remove();
									
					var cartTotalObj    = $('#cartTotal');
					var originCartTotal = parseFloat(cartTotalObj.html());					
					var newCartTotal    = originCartTotal + changeItemTotal;
					cartTotalObj.html(newCartTotal.toFixed(2));				
				}
			}
		});
	//}
    });

	return false;
}
</script>